//
//  SetViewController.m
//  FuncFanc
//
//  Created by hubing on 2018/3/5.
//  Copyright © 2018年 huhu. All rights reserved.
//

#import "FontViewController.h"

@interface FontViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) NSArray *fontList;
@property (nonatomic,strong) NSDictionary *fontDict;

@end

@implementation FontViewController
- (void)viewDidLoad {
    self.title = @"字体";
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self readFontDataFromPlist];
    _tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableview.backgroundColor = [UIColor clearColor];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];

}

-(void)readFontDataFromPlist{
      NSMutableArray *tempArray = [NSMutableArray array];
     NSString *path = [[NSBundle mainBundle] pathForResource:@"FontList" ofType:@"plist"];
     _fontDict = [NSDictionary dictionaryWithContentsOfFile:path];
    for (NSString *key in _fontDict) {
        [tempArray addObject:key];
    }
    static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch |
    NSWidthInsensitiveSearch | NSForcedOrderingSearch;
    NSLocale *currentLocale = [NSLocale currentLocale];
    NSComparator finalList = ^(id string1,id string2){
        NSRange string1Range = NSMakeRange(0, [string1 length]);
        return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale];
    };
     _fontList = [tempArray sortedArrayUsingComparator:finalList];
    NSLog(@"%@",_fontDict);
    NSLog(@"%@",_fontList);
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _fontList.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array = [_fontDict objectForKey:_fontList[section]];
    return array.count;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return _fontList[section];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ziticell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ziticell"];
    }
     NSArray *array = [_fontDict objectForKey:_fontList[indexPath.section]];
//    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",array[indexPath.row]];
    cell.textLabel.font = [UIFont fontWithName:cell.textLabel.text size:20];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",array[indexPath.row]];
    return cell;
}




@end
